function [nchan] = thist2xls(xmin,xmax)

%  Initially copied from analysis2xls on 5/3/04 by Brian Ross
%  This function will create an Excel file containing time history records
%  from the Project structure.
%
%  On 5/26/04 modified routine to include the pass of xmin and xmax from the
%  shell program.  This allows the export program to export only the time segment
%  that has been selected in the shell window.
%
%  On 1/11/05 cleaned up some comments.
%
%
%  Last modified 1/11/05 by B. Ross
%
%  *****************************************************************************
%  [number_of_channels_written] = thist2xls(xmin,xmax)
%
%  num_records_written = number of records(channels) written to Excel file
%                 xmin = export start time selected in main Shell window
%                 xmax = export end time selected in main Shell window
% ********************************************************************************

global Project;

nchan = 0;

%  Select Channels to Export

[fchan, lchan, varargout] = channel_select;
nchan = lchan-fchan;

%  Prompt for Excel filename and directory using the Project
%  Setup Filename as the default name.

[fname, pname] = uiputfile([Project.Test_Item.Run.Setup_File '.xls'],...
                    'Select Export File Name');
if isequal([fname, pname], [0,0])
    return
end
filename = fullfile(pname, fname);

%  Generate the ordinate & abscissa matrices

for ii = fchan:lchan
    ordinate(:,ii-fchan+1) = Project.Test_Item.Run.Channel(ii).Time_His(xmin:xmax).';
end
abscissa = Project.Test_Item.Run.Time_Ref(xmin:xmax).';

%  Combine abscissa and ordinate data into a single matrix

m = [abscissa,ordinate];
        
%  Generate the vector of column headers

for kk = fchan:lchan
    respnam{1,kk-fchan+2} = [Project.Test_Item.Run.Channel(kk).Coordinate ...
                           Project.Test_Item.Run.Channel(kk).Direction ...
                           Project.Test_Item.Run.Channel(kk).Orientation];
end
    respnam{1,1} = 'Time';

%  For now, set the header matrix to the following:

head = ['time, ampl chan 1, ampl chan 2, ...'];


%  Generate the file

xlswrite(m,head,respnam,filename);